package com.fish.anne.webapp.action.front;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.fish.anne.Constants;
import com.fish.anne.model.Member;
import com.fish.anne.service.MemberManager;
import com.fish.anne.webapp.action.BaseAction;
import com.fish.anne.webapp.util.WebContextKey;
import com.opensymphony.xwork2.Preparable;

public class MemberAction extends BaseAction implements Preparable {
    private MemberManager memberManager;
    private List members;
    private Member member;
    private Long  memberId;

    
    
    public void setMemberManager(MemberManager memberManager) {
        this.memberManager = memberManager;
    }

    public List getMembers() {
        return members;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    /*
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String memberId = getRequest().getParameter("member.memberId");
            if (memberId != null && !memberId.equals("")) {
                member = memberManager.get(new Long(memberId));
            }
        }
    }
*/
    public String list() {
        members = memberManager.getAll();
        return SUCCESS;
    }

    public void setMemberId(Long  memberId) {
        this. memberId =  memberId;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public String delete() {
        memberManager.remove(member.getMemberId());
        saveMessage(getText("member.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (memberId != null) {
            member = memberManager.get(memberId);
        } else {
            member = new Member();
        }

        return SUCCESS;
    }
    
    public String signIn() throws Exception {
    	System.out.println("进来了:");
		String email = this.getRequest().getParameter("email");
		String password = this.getRequest().getParameter("password");
		String tel = this.getRequest().getParameter("tel");
		//System.out.println("name:"+name);
    	//return SUCCESS;
		Member member = new Member();
		member.setEmail(email);
		member.setUname(email);
		member.setPassword(password);
		member.setMobile(tel);
		member.setAddr("");
		member.setArea("");
		member.setBDay((byte)0);
		member.setBMonth((byte)0);
		member.setBYear((short)0);
		member.setCity("");
		member.setEducation("");
		member.setInterest("");
		member.setLoginCount(0L);
		member.setMemberLvId(1L);
		member.setName("");
		member.setOrderNum(0L);
		member.setPasswordAnswer("");
		member.setPasswordQuestion("");
		member.setPoint(0L);
		member.setPointFreeze(0L);
		member.setPointHistory(0L);
		member.setProvince("");
		member.setRegIp(this.getRequest().getRemoteAddr());
		member.setRegTime(System.currentTimeMillis());
		member.setSex(true);
		member.setState((byte)0);
		member.setTel("");
		member.setVocation("");
		member.setWedlock(true);
		member.setZip("");
		
		
		
		
		String warningMessage = memberManager.saveMember(member);
		if(warningMessage!=null){
			HttpServletResponse response = this.getResponse();
			PrintWriter out = response.getWriter();
			out.println(warningMessage);
			out.flush();
			out.close();
		}
		return null;
    }
    
    @SuppressWarnings("unchecked")
	public String login() throws Exception {
 
    	member = memberManager.login(member);
		if(member ==null){					 
			return INPUT;
		}
		
		session.put(WebContextKey.MEMBER, member);
		forward = (String) session.get(Constants.FORWARD);
		if (forward != null) {
			return Constants.FORWARD;
		}
		
    	return SUCCESS;
    }
    
    public void changePass() throws Exception{
    	String userid =this.getRequest().getSession().getAttribute("userId").toString();
    	String oldPass = this.getRequest().getParameter("oldPass");
    	String newPass = this.getRequest().getParameter("newPass");
    	String warningMessage = memberManager.changePassword(userid, oldPass, newPass);
		if(warningMessage!=null){
			HttpServletResponse response = this.getResponse();
			PrintWriter out = response.getWriter();
			out.println(warningMessage);
			out.flush();
			out.close();
		}
    }

    public String save() throws Exception {
 
        boolean isNew = (member.getMemberId() == null);

        memberManager.save(member);

        String key = (isNew) ? "member.added" : "member.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	}
}